package com.lty.jeeg.server.modules.fas.service.impl;

import com.lty.jeeg.server.common.page.table.PageTableHandler;
import com.lty.jeeg.server.common.page.table.PageTableRequest;
import com.lty.jeeg.server.common.page.table.PageTableResponse;
import com.lty.jeeg.server.common.utils.BeanConverter;
import com.lty.jeeg.server.modules.fas.dto.SupplierDTO;
import com.lty.jeeg.server.modules.fas.vo.SupplierVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lty.jeeg.server.modules.fas.dao.SupplierDao;
import com.lty.jeeg.server.modules.fas.model.Supplier;
import com.lty.jeeg.server.modules.fas.service.SupplierService;

import java.util.Date;

/**
 * @author : LTY
 * @date : 2019-02-12 10:01:41
 * Description : 供应商
 */
@Service
@Slf4j
public class SupplierServiceImpl implements SupplierService {

    private final SupplierDao supplierDao;

    @Autowired
    public SupplierServiceImpl(SupplierDao supplierDao) {
        this.supplierDao = supplierDao;
    }

    @Override
    public Supplier getById(Long id) {
        return supplierDao.getById(id);
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
	public int delete(Long id) {
		log.debug("删除Supplier=>{}", id);
		return supplierDao.delete(id);
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
	public int update(SupplierDTO supplierDTO) {
        supplierDTO.preUpdate();
        log.debug("修改Supplier=>{}", supplierDTO.getId());
        return supplierDao.update(BeanConverter.map(supplierDTO, Supplier.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long save(SupplierDTO supplierDTO) {
        supplierDTO.preInsert();
        Long supplierId= supplierDao.save(BeanConverter.map(supplierDTO, Supplier.class));
        log.debug("新增Supplier=>{}", supplierId);
        return supplierId;
    }

    @Override
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(
                request1 -> supplierDao.count(request1.getParams()),
                request2 -> supplierDao.query(
                        request2.getParams(), request2.getOffset(), request2.getLimit()))
                .handle(request);
    }



}
